package lily.com.app;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

	private TaskListViewAdapter adpater = null;
	private TaskListView listView = null;
	boolean isFirstLoad = false;
//	private static int cnt = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		if(!SQLiteManager.isOpen()) SQLiteManager.open(this);
		
		SharedPreferences pref = this.getSharedPreferences("PREFERENCE",Context.MODE_WORLD_WRITEABLE);  
		SharedPreferences.Editor prefEditor = pref.edit();
		isFirstLoad = pref.getBoolean("isFirstLoad", true);
		if(isFirstLoad){
			prefEditor.putBoolean("isFirstLoad", false);
			prefEditor.commit();
		}
		
		setupViews();
	}
	
	@Override
	protected void onResume() {
//		Log.d("DEBUG", "onResume()");
		if(!SQLiteManager.isOpen()) SQLiteManager.open(this);
		adpater = TaskListViewAdapter.getInstance(this);
		//TODO for debug: need to delete
		adpater.selectAllTask();
//		adpater.insertTask("欢迎使用ClearList" + String.valueOf(cnt++), "使用介绍", TaskDomain.TaskStatus.NONE);
		if(isFirstLoad){
			adpater.insertTask("欢迎使用ClearList", "如何使用ClearList?\n请按下手机的菜单键\n进入新手指导", TaskDomain.TaskStatus.NONE);
			isFirstLoad = false;
		}
		if(adpater.getCount() == 0){
			adpater.insertItem(0);
		}
		GestureListener guestureListener = new GestureListener();
		guestureListener.setGestureResponder(new TaskListViewGestureResponder());
		listView.setInterceptTouchListner(guestureListener);
		listView.setLongClickable(true);
		listView.setAdapter(adpater);
//		adpater.notifyDataSetChanged();
		super.onResume();
	}
	
	@Override
	protected void onPause() {
//		Log.d("DEBUG", "onPause()");
		adpater = TaskListViewAdapter.getInstance(this);
		adpater.flushRowsIntoDB();
		super.onPause();
	}
	
	public void setupViews() {
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.setOrientation(LinearLayout.VERTICAL);

//		LinearLayout.LayoutParams wrapLayout = new LinearLayout.LayoutParams(
//				ViewGroup.LayoutParams.WRAP_CONTENT,
//				ViewGroup.LayoutParams.WRAP_CONTENT);
//		LinearLayout.LayoutParams fillLayout = new LinearLayout.LayoutParams(
//				ViewGroup.LayoutParams.FILL_PARENT,
//				ViewGroup.LayoutParams.FILL_PARENT);
		LinearLayout.LayoutParams verticalLayout = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);

		listView = new TaskListView(this);
		listView.setDividerHeight(1);
		listView.setId(R.id.lvcList);
		linearLayout.addView(listView, verticalLayout);
		setContentView(linearLayout);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.help:
			Intent intent = new Intent();
        	intent.setClass(this, GuideFirstActivity.class);
        	startActivity(intent);
        	break;
        default:
        	return false;
		}
		return true;
	}
	
	@Override
	protected void onDestroy() {
		if(SQLiteManager.isOpen()) SQLiteManager.close();
		super.onDestroy();
	}
}
